// bindPhoneNum.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isdisable: false,
        verifyInfo: '获取验证码',
        bgColor: '#fff'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },
    phoneNum: function(e) {
        var that = this;
        var phone = e.detail.value;
        //正则匹配验证匹配手机号


        that.setData({
            phone: phone
        })



    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },

    huoquyanzhengma: function() {
        var that = this;

        //表单验证电话号码，做倒计时
        var count = 60;
        var re = /^1[3|4|5|7|8][0-9]\d{4,8}$/;
        if (!re.test(that.data.phone)) {
            wx.showModal({
                title: "请输入正确的手机号"
            });
            return false;
        } else {
            if (that.data.isdisable == false) {


                //倒计时定时器
                var timer = setInterval(function() {
                    count--;
                    if (count >= 1) {
                        that.setData({
                            verifyInfo: '倒计时' + count + 's',
                            isdisable: true,
                            bgColor: '#dadada'
                        })
                    } else {

                        clearInterval(timer);
                        that.setData({
                            verifyInfo: '获取验证码',
                            isdisable: false,
                            bgColor: '#fff'
                        })
                    }
                }, 1000);
                //=============================================================================
                var phone = that.data.phone;
                ////请求接口，获取验证码
                wx.request({
                    url: app.data.baseUrl + '/AibraUsb/userInfo/bindVerify.do',
                    data: {
                        mobile: phone
                    },
                    success: function(res) {
                        if (res.data.msg == 1) {
                            wx.showToast({
                                duration: 3000,
                                title: "短信验证码已发送"
                            })
                            app.globalData.sms = res.data.sms;
                            that.setData({

                                phoneNum: res.data.mobile
                            })
                        }
                        if (res.data.msg == 0) {
                            wx.showToast({
                                duration: 3000,
                                title: "手机号已绑定过"
                            })
                        }

                    }

                })



            }
        }
    },
    bangding: function() {
        console.log(app.globalData.openid)
        var that = this;
        if (that.data.phoneNum) {
            wx.request({
                url: app.data.baseUrl + '/AibraUsb/userInfo/userMobile.do',
                data: {
                    openId: app.globalData.openid,
                    mobile: that.data.phoneNum
                },
                success: function(res) {
                    console.log(res)
                    wx.showToast({
                            title: '绑定成功'
                        })
                        // wx.navigateBack({
                        //         detail: 1
                        //     })
                    wx.switchTab({
                        url: '../personal?phoneNum=' + that.data.phoneNum
                    })
                }
            })
        } else {
            wx.showToast({
                title: '请输入手机号'
            })
        }

    },
    yanzhengmatianjia(e) {
        var that = this;
        console.log(e.detail.value)
        console.log(that.data.sms)

        console.log(e.detail.value);
        that.setData({
            sms: e.detail.value
        })
    }
})